package luke4fun.wheresmymoney.adapter.list.impl;

import android.app.Activity;
import android.content.Context;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.bean.AccBean;
import luke4fun.wheresmymoney.bean.AccDtlBean;
import luke4fun.wheresmymoney.bean.DayAccBean;
import luke4fun.wheresmymoney.util.FormatUtil;
import luke4fun.wheresmymoney.viewholder.AccListViewHolder;

public class MonthAccAdapter extends BaseExpandableListAdapter {
	
	private List<DayAccBean> recordList;
	private LayoutInflater inflater;
	private Context context;
	
	private ActionMode mActionMode = null;
	private ActionMode.Callback mActionModeCallback;
	
	public MonthAccAdapter(Context context) {
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		recordList = new ArrayList<DayAccBean>();
		initActionModeListener();
	}

	public void updateList(List<DayAccBean> dataList) {
		if (!recordList.containsAll(dataList)) {
			recordList.clear();
			recordList.addAll(dataList);
			notifyDataSetChanged();
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return recordList == null ? null : recordList.get(groupPosition).getAcc(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		
		AccListViewHolder holder;
		
		// set the view holder for reuse
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.acc_list, null);
			holder = new AccListViewHolder();
			holder.category = (TextView) convertView.findViewById(R.id.acc_col_category);
			holder.sum = (TextView) convertView.findViewById(R.id.acc_col_sum);
			holder.accDtlList = (LinearLayout) convertView.findViewById(R.id.acc_dtl_list);
			
			convertView.setOnLongClickListener(new View.OnLongClickListener() {
			    // Called when the user long-clicks on someView
			    public boolean onLongClick(View view) {
			        if (mActionMode != null) {
			            return false;
			        }

			        // Start the CAB using the ActionMode.Callback defined above
			        mActionMode = ((Activity) context).startActionMode(mActionModeCallback);
			        view.setSelected(true);
			        return true;
			    }
			});
			convertView.setTag(holder);
		} else {
			holder = (AccListViewHolder) convertView.getTag();
		}

		holder.accDtlList.removeAllViews();
		// set text view content
		AccBean acc = (AccBean) getChild(groupPosition, childPosition);
		if (acc != null) {
			for (Iterator<AccDtlBean> it=acc.getDtlList().iterator(); it.hasNext();) {
				
				AccDtlBean dtlBean = it.next();
				for (AccDtlBean dtl : dtlBean.getInfoList()) {
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
					TextView item = new TextView(context);
					item.setTextAppearance(context, R.style.normalFont);
					item.setText(dtl.getItem());
					item.setLayoutParams(params);
					
					LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
					TextView price = new TextView(context);
					price.setGravity(Gravity.RIGHT);
					price.setTextAppearance(context, R.style.normalFont);
					if (dtl.getPrice() != null) {
						price.setText(String.valueOf(dtl.getPrice()));
					}
					price.setLayoutParams(params2);
					
					LinearLayout lLayout = new LinearLayout(context);
					lLayout.setOrientation(LinearLayout.HORIZONTAL);
					lLayout.addView(item);
					lLayout.addView(price);
					
					holder.accDtlList.addView(lLayout);
				}
				
			}
			
			if (acc.getCategory() != null && acc.getCategory().length() > 0) {
				holder.category.setText(acc.getCategory());
			}
			holder.sum.setText(String.valueOf( acc.getSum() ));
		}
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return recordList == null ? 0 : recordList.get(groupPosition).getAccMap().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return recordList == null ? null : recordList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return recordList == null ? 0 : recordList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		AccListViewHolder holder;
		
		// set the view holder for reuse
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.month_acc_list, null);
			holder = new AccListViewHolder();
			holder.price = (TextView) convertView.findViewById(R.id.month_col_price);
			holder.expenseDate = (TextView) convertView.findViewById(R.id.month_col_expenseDate);
			
			convertView.setTag(holder);
		} else {
			holder = (AccListViewHolder) convertView.getTag();
		}

		DayAccBean acc = recordList.get(groupPosition);
		if (acc != null) {
			holder.price.setText(FormatUtil.formatNumber(acc.getPrice()));

			Date d = acc.getExpenseDate();
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			int day = c.get(Calendar.DAY_OF_MONTH) ;
			String dayStr = day < 10 ? "0" + day : "" + day;
			dayStr = c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()) + " " + dayStr;
			holder.expenseDate.setText(dayStr);
		}
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
	
	private void initActionModeListener() {
		mActionModeCallback = new ActionMode.Callback() {

			// Called when the action mode is created; startActionMode() was called
			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				// TODO Auto-generated method stub
				return true;
			}

			// Called each time the action mode is shown. Always called after onCreateActionMode, but
		    // may be called multiple times if the mode is invalidated.
			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				// TODO Auto-generated method stub
				return false;
			}
			
			// Called when the user selects a contextual menu item
			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				// TODO Auto-generated method stub
				return false;
			}

			// Called when the user exits the action mode
			@Override
			public void onDestroyActionMode(ActionMode mode) {
				mActionMode = null;
			}
			
		};
	}
}
